package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.SendSMS
import me.leiusual.sharing.pref.User
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.mine.model.ParamsMobileUpdate
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/25
 */
class MineMobileViewModel @Inject constructor(
    private val userRepository: UserRepository,
    val user: User
) : ViewModel() {

  private val _sendSMSOld = MutableLiveData<String>()
  val sendSMSOld: LiveData<Data<Unit>> = Transformations.switchMap(_sendSMSOld) {
    userRepository.sendSMS(SendSMS.Type.UPDATE_MOBILE, it).toLiveData()
  }

  fun sendSMSOld(mobile: String?) {
    _sendSMSOld.value = mobile ?: return
  }

  private val _sendSMS = MutableLiveData<String>()
  val sendSMS: LiveData<Data<Unit>> = Transformations.switchMap(_sendSMS) {
    userRepository.sendSMS(SendSMS.Type.UPDATE_MOBILE, it).toLiveData()
  }

  fun sendSMS(mobile: String) {
    _sendSMS.value = mobile
  }

  private val _mobileUpdate = MutableLiveData<ParamsMobileUpdate>()
  val mobileUpdate: LiveData<Data<Unit>> = Transformations.switchMap(_mobileUpdate) {
    userRepository.mobileUpdate(it).toLiveData()
  }

  fun mobileUpdate(verifyCode: String, mobile: String, checkCode: String) {
    _mobileUpdate.value = ParamsMobileUpdate(verifyCode, mobile, checkCode)
  }
}